package game;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CharacterSelectPanel extends JPanel {
    private GameWindow gameWindow;
    private List<String> characters;
    private JTextArea characterInfoArea;
    private JLabel characterImageLabel;

    public CharacterSelectPanel(GameWindow gameWindow) {
        this.gameWindow = gameWindow;
        setLayout(new BorderLayout(10, 10));
        setBackground(Color.BLACK);

        // 初始化角色列表
        characters = new ArrayList<>();
        characters.add("草薙京");
        characters.add("八神庵");
        /*
        characters.add("不知火舞");
        characters.add("大门五郎");
        characters.add("二阶堂红丸");
        */

        // 标题
        JLabel titleLabel = new JLabel("选择你的角色", SwingConstants.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 36));
        titleLabel.setForeground(Color.RED);
        titleLabel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
        add(titleLabel, BorderLayout.NORTH);

        // 主内容面板
        JPanel contentPanel = new JPanel(new BorderLayout(20, 20));
        contentPanel.setOpaque(false);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));

        // 角色选择按钮面板
        JPanel characterPanel = new JPanel(new GridLayout(1, characters.size(), 20, 0));
        characterPanel.setOpaque(false);

        // 角色信息展示面板
        JPanel infoPanel = new JPanel(new BorderLayout());
        infoPanel.setOpaque(false);
        infoPanel.setBorder(BorderFactory.createLineBorder(Color.RED, 2));

        // 角色图片展示
        characterImageLabel = new JLabel("", SwingConstants.CENTER);
        characterImageLabel.setPreferredSize(new Dimension(200, 300));
        infoPanel.add(characterImageLabel, BorderLayout.WEST);

        // 角色信息文本区域
        characterInfoArea = new JTextArea();
        characterInfoArea.setEditable(false);
        characterInfoArea.setLineWrap(true);
        characterInfoArea.setWrapStyleWord(true);
        characterInfoArea.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        characterInfoArea.setForeground(Color.WHITE);
        characterInfoArea.setBackground(Color.BLACK);
        characterInfoArea.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        JScrollPane scrollPane = new JScrollPane(characterInfoArea);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setBorder(null);

        infoPanel.add(scrollPane, BorderLayout.CENTER);

        // 创建角色按钮
        for (String character : characters) {
            JButton charButton = createCharacterButton(character);
            characterPanel.add(charButton);
        }

        contentPanel.add(characterPanel, BorderLayout.NORTH);
        contentPanel.add(infoPanel, BorderLayout.CENTER);

        // 开始游戏按钮
        JButton startButton = new JButton("开始战斗");
        startButton.setFont(new Font("微软雅黑", Font.BOLD, 20));
        startButton.setForeground(Color.WHITE);
        startButton.setBackground(Color.RED);
        startButton.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
        startButton.addActionListener(e -> {
            if (characterInfoArea.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "请先选择一个角色", "提示", JOptionPane.WARNING_MESSAGE);
                return;
            }

            Random random = new Random();
            String enemyCharacter;
            do {
                enemyCharacter = characters.get(random.nextInt(characters.size()));
            } while (enemyCharacter.equals(characterInfoArea.getText().split("\n")[0]));

            gameWindow.startFight(characterInfoArea.getText().split("\n")[0], enemyCharacter);
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.setOpaque(false);
        buttonPanel.add(startButton);
        contentPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(contentPanel, BorderLayout.CENTER);
    }

    private JButton createCharacterButton(String character) {
        JButton button = new JButton(character);
        button.setFont(new Font("微软雅黑", Font.BOLD, 18));
        button.setForeground(Color.WHITE);
        button.setBackground(Color.DARK_GRAY);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
        button.setPreferredSize(new Dimension(150, 60));

        button.addActionListener(e -> showCharacterInfo(character));
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(Color.RED);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(Color.DARK_GRAY);
            }
        });

        return button;
    }

    private void showCharacterInfo(String character) {
        // 清空之前的信息
        characterInfoArea.setText("");
        characterImageLabel.setIcon(null);

        // 设置角色信息
        String info = "";
        ImageIcon icon = null;

        try {
            switch (character) {
                case "草薙京":
                    info = "草薙京\n\n";
                    info += "流派：草薙流古武术\n";
                    info += "出生地：日本\n";
                    info += "年龄：20岁\n";
                    info += "身高：181cm\n";
                    info += "体重：75kg\n";
                    info += "血型：B(RH+)\n";
                    info += "兴趣：写诗\n";
                    info += "喜欢的食物：烤鱼\n";
                    info += "重要的事物：摩托车、女友小雪\n";
                    info += "讨厌的事物：努力\n\n";
                    info += "战斗属性：移速4，攻击力12";

                    // 加载图片 (确保图片路径正确)
                    InputStream is = getClass().getResourceAsStream("/characters/kyo/kyo.jpg");
                    try {
                        if (is != null) {
                            icon = new ImageIcon(ImageIO.read(is));
                        }else {
                            System.err.println("无法加载图片资源");
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    break;

                case "八神庵":
                    info = "八神庵\n\n";
                    info += "流派：八神流古武术+本能\n";
                    info += "出生地：日本\n";
                    info += "年龄：20岁\n";
                    info += "身高：182cm\n";
                    info += "体重：76kg\n";
                    info += "血型：O(RH+)\n";
                    info += "兴趣：乐队活动\n";
                    info += "喜欢的食物：肉\n";
                    info += "重要的事物：新买的靴子\n";
                    info += "讨厌的事物：草薙京\n\n";
                    info += "战斗属性：移速3，攻击力13";

                    // 加载图片 (确保图片路径正确)
                    InputStream is2 = getClass().getResourceAsStream("/characters/iori/iori.jpg");
                    try {
                        if (is2 != null) {
                            icon = new ImageIcon(ImageIO.read(is2));
                        }else {
                            System.err.println("无法加载图片资源");
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    break;
            }

            characterInfoArea.setText(info);
            if (icon != null) {
                // 缩放图片以适应标签
                Image image = icon.getImage().getScaledInstance(200, 170, Image.SCALE_SMOOTH);
                characterImageLabel.setIcon(new ImageIcon(image));
            }
        } catch (Exception e) {
            characterInfoArea.setText("无法加载角色信息: " + e.getMessage());
            e.printStackTrace();
        }
    }
}